package com.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.configer.RedisConfig;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class Utils {
    private static final String HOST = "localhost";
    private static final int PORT = 6379;
    private static final int TIMEOUT = 5000;
    private static final String PASSWORD = "66655260";
    private static final String ALI_DOMAIN = "https://magicnotesoss.oss-cn-nanjing.aliyuncs.com/";
    private static final RedisConfig redisConfig = new RedisConfig();
    private static final JedisPool jedisPool = redisConfig.jedisPool();
    @Value("${aliyun.oss.endpoint}")
    private String tempEndpoint;

    @Value("${aliyun.oss.access-key-id}")
    private String tempAccessKeyId;

    @Value("${aliyun.oss.access-secret}")
    private String tempAccessSecret;

    @Value("${aliyun.oss.bucket-name}")
    private String tempBucketName;

    private static String endpoint;
    private static String accessKeyId;
    private static String accessSecret;
    private static String bucketName;

    @PostConstruct
    public void init() {
        endpoint = tempEndpoint;
        accessKeyId = tempAccessKeyId;
        accessSecret = tempAccessSecret;
        bucketName = tempBucketName;
    }
    public static Map<String, Object> createMap(Object... keyValuePairs) {
        if (keyValuePairs.length % 2 != 0) {
            throw new IllegalArgumentException("传入参数个数必须为偶数");
        }

        Map<String, Object> resultMap = new HashMap<>();

        for (int i = 0; i < keyValuePairs.length; i += 2) {
            String key = (String) keyValuePairs[i];
            Object value = keyValuePairs[i + 1];
            resultMap.put(key, value);
        }
        return resultMap;

    }


    public static Integer getTokenUserId(String token) {
        try (Jedis jedis = jedisPool.getResource()) {
            String userId = jedis.hget(token, "user_id");
            if (userId != null) {
                return Integer.parseInt(userId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String uploadImage(MultipartFile file) throws IOException {
        //生成文件名
        String originalFileName = file.getOriginalFilename();//原来的图片名
        String ext = "." + FilenameUtils.getExtension(originalFileName);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + ext;


        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessSecret);
        ossClient.putObject("magicnotesoss", fileName, file.getInputStream());
        ossClient.shutdown();
        return fileName;
    }

    public static String downloadImage(String fileName) {
        // 创建OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessSecret);
        System.out.println("bucketName:" + bucketName);
        System.out.println("filename:" + fileName);

        // 构建图片URL
        String imageUrl = "https://" + bucketName + "." + endpoint.replace("http://", "") + "/" + fileName;

        // 关闭OSS客户端
        ossClient.shutdown();

        return imageUrl;
    }

    public static String generateImageUrl(String fileName) {
        return "https://" + bucketName + "." + endpoint.replace("http://", "") + "/" + fileName;
    }
}
